import { Component, OnInit } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { environment } from 'src/environments/environment';
import { pb } from 'src/pb';
import { ConfigPage } from '../config/config.page';
import { HttpService } from '../http.service';

@Component({
  selector: 'app-home',
  templateUrl: './home.page.html',
  styleUrls: ['./home.page.scss'],
})
export class HomePage implements OnInit {
  name = '';
  config: pb.IConfig = {};
  constructor(private http: HttpService, private modalCtrl: ModalController) {}

  async ngOnInit() {
    await this.http.auth();
    this.config = await this.http.get('/api/config');
    this.name = environment.user;
  }

  async edit() {
    const modal = await this.modalCtrl.create({
      component: ConfigPage,
      componentProps: {
        config: this.config,
      },
    });
    await modal.present();
    const { data } = await modal.onDidDismiss();
    if (data) {
      this.config = data;
    }
  }
}
